﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HartConfigTool.Commands.SystemCMDs;
using HartConfigTool.CommandsQueue;
using HartConfigTool.HARTCommands.CMDs;
using HartConfigTool.UI;
using HartConfigTool.UI.DebugWindow;
using HartConfigTool.UI.ExpandControls;

namespace HartConfigTool
{
    public partial class CommandDebugForm : Form
    {
        public CommandDebugForm()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        private void CommandDebugForm_Load(object sender, EventArgs e)
        {
            // Commands Combox Init
            foreach (KeyValuePair<int, string> kvp in ObserversInfo.HARTCMDsID)
            {
                this.cboxUniversalCmd.Items.Add(kvp.Value);
            }
            this.cboxUniversalCmd.SelectedIndex = 0;

            // Command0 paramters display
            dataGridViewCmd0.RowCount = HARTProtocolV7.Cmd0ParameterList.Length + 1;
            for (int i = 0; i < HARTProtocolV7.Cmd0ParameterList.Length; i++)
            {
                dataGridViewCmd0.Rows[i].Cells[0].Value = HARTProtocolV7.Cmd0ParameterList[i];
            }
            // Command#6
            foreach (KeyValuePair<byte, string> kvp in HARTProtocolV7.LoopCurrentModeCodes)
            {
                this.cboxCMD6LoopCurMode.Items.Add(kvp.Value);
            }
            this.cboxCMD6LoopCurMode.SelectedIndex = 0;
            this.cboxPollingAddr.SelectedIndex = 0;
        }
        private async void btnUniversalCmdSend_Click(object sender, EventArgs e)
        {
            if (cboxUniversalCmd.Text == ObserversInfo.HARTCMDsID[0])
            {
                CMDsDebugWinHARTCMD0 debugWinCMD0 = new CMDsDebugWinHARTCMD0(MainForm.serialPortsManager);

                debugWinCMD0.RegisterObserver(ref debugWinCMD0.CMD0Observers, ObserversInfo.HARTCMD0Observers["All"], new GridViewUpdater(this.dataGridViewCmd0));
                debugWinCMD0.RegisterObserver(ref debugWinCMD0.CMD0Observers, ObserversInfo.HARTCMD0Observers["HART Rev"], new TextBoxUpdater(this.txtBoxHARTRev));
                debugWinCMD0.RegisterObserver(ref debugWinCMD0.CMD0Observers, ObserversInfo.HARTCMD0Observers["Device ID"], new TextBoxUpdater(this.txtBoxDeviceID));
                debugWinCMD0.RegisterObserver(ref debugWinCMD0.CMD0Observers, ObserversInfo.HARTCMD0Observers["Manufacturer ID"], new TextBoxUpdater(this.txtBoxManufaID));

                await debugWinCMD0.ExecuteCMD();
            }
            else if (cboxUniversalCmd.Text == ObserversInfo.HARTCMDsID[1])
            {
                CMDsDebugWinHARTCMD1 debugWinCMD1 = new CMDsDebugWinHARTCMD1(MainForm.serialPortsManager);

                debugWinCMD1.RegisterObserver(ref debugWinCMD1.CMD1Observers, ObserversInfo.HARTCMD1Observers["PV Unit"], new TextBoxUpdater(this.cmd1UnitDisp));
                debugWinCMD1.RegisterObserver(ref debugWinCMD1.CMD1Observers, ObserversInfo.HARTCMD1Observers["PV"], new TextBoxUpdater(this.cmd1PreDisplay));

                await debugWinCMD1.ExecuteCMD();
            }
            else if (cboxUniversalCmd.Text == ObserversInfo.HARTCMDsID[2])
            {
                CMDsDebugWinHARTCMD2 debugWinCMD2 = new CMDsDebugWinHARTCMD2(MainForm.serialPortsManager);

                debugWinCMD2.RegisterObserver(ref debugWinCMD2.CMD2Observers, ObserversInfo.HARTCMD2Observers["PV Loop Current"], new TextBoxUpdater(this.txtBoxLoopCurrent));
                debugWinCMD2.RegisterObserver(ref debugWinCMD2.CMD2Observers, ObserversInfo.HARTCMD2Observers["PV Percent of Range"], new TextBoxUpdater(this.txtBoxPerOfRge));

                await debugWinCMD2.ExecuteCMD();
            }
            else if (cboxUniversalCmd.Text == ObserversInfo.HARTCMDsID[3])
            {
                CMDsDebugWinHARTCMD3 debugWinCMD3 = new CMDsDebugWinHARTCMD3(MainForm.serialPortsManager);

                debugWinCMD3.RegisterObserver(ref debugWinCMD3.CMD3Observers, ObserversInfo.HARTCMD3Observers["PV Loop Current"], new TextBoxUpdater(this.txtBoxPVLoopCur));
                debugWinCMD3.RegisterObserver(ref debugWinCMD3.CMD3Observers, ObserversInfo.HARTCMD3Observers["PV Units Code"], new TextBoxUpdater(this.txtBoxPVUnit));
                debugWinCMD3.RegisterObserver(ref debugWinCMD3.CMD3Observers, ObserversInfo.HARTCMD3Observers["PV"], new TextBoxUpdater(this.txtBoxPV));
                debugWinCMD3.RegisterObserver(ref debugWinCMD3.CMD3Observers, ObserversInfo.HARTCMD3Observers["SV Units Code"], new TextBoxUpdater(this.txtBoxSVUnitCode));
                debugWinCMD3.RegisterObserver(ref debugWinCMD3.CMD3Observers, ObserversInfo.HARTCMD3Observers["SV"], new TextBoxUpdater(this.txtBoxSV));
                debugWinCMD3.RegisterObserver(ref debugWinCMD3.CMD3Observers, ObserversInfo.HARTCMD3Observers["TV Units Code"], new TextBoxUpdater(this.txtBoxTVUnitCode));
                debugWinCMD3.RegisterObserver(ref debugWinCMD3.CMD3Observers, ObserversInfo.HARTCMD3Observers["TV"], new TextBoxUpdater(this.txtBoxTV));
                debugWinCMD3.RegisterObserver(ref debugWinCMD3.CMD3Observers, ObserversInfo.HARTCMD3Observers["QV Units Code"], new TextBoxUpdater(this.txtBoxQVUnitCode));
                debugWinCMD3.RegisterObserver(ref debugWinCMD3.CMD3Observers, ObserversInfo.HARTCMD3Observers["QV"], new TextBoxUpdater(this.txtBoxQV));

                await debugWinCMD3.ExecuteCMD();
            }
            else if (cboxUniversalCmd.Text == ObserversInfo.HARTCMDsID[6])
            {
                CMDsDebugWinHARTCMD6 debugWinCMD6 = new CMDsDebugWinHARTCMD6(MainForm.serialPortsManager, this.cboxPollingAddr.Text, this.cboxCMD6LoopCurMode.Text);
                await debugWinCMD6.ExecuteCMD();
            }
            else if (cboxUniversalCmd.Text == ObserversInfo.HARTCMDsID[7])
            {
                CMDsDebugWinHARTCMD7 debugWinCMD7 = new CMDsDebugWinHARTCMD7(MainForm.serialPortsManager);

                debugWinCMD7.RegisterObserver(ref debugWinCMD7.CMD7Observers, ObserversInfo.HARTCMD7Observers["Polling Addr"], new TextBoxUpdater(this.txtBoxCMD7PollingAddr));
                debugWinCMD7.RegisterObserver(ref debugWinCMD7.CMD7Observers, ObserversInfo.HARTCMD7Observers["Loop Current Mode"], new TextBoxUpdater(this.txtBoxCMD7LoopCurMode));

                await debugWinCMD7.ExecuteCMD();
            }
            else if (cboxUniversalCmd.Text == ObserversInfo.HARTCMDsID[8])
            {
                CMDsDebugWinHARTCMD8 debugWinCMD8 = new CMDsDebugWinHARTCMD8(MainForm.serialPortsManager);

                debugWinCMD8.RegisterObserver(ref debugWinCMD8.CMD8Observers, ObserversInfo.HARTCMD8Observers["PV Cls"], new TextBoxUpdater(this.txtBoxCMD8PVCls));
                debugWinCMD8.RegisterObserver(ref debugWinCMD8.CMD8Observers, ObserversInfo.HARTCMD8Observers["SV Cls"], new TextBoxUpdater(this.txtBoxCMD8SVCls));
                debugWinCMD8.RegisterObserver(ref debugWinCMD8.CMD8Observers, ObserversInfo.HARTCMD8Observers["TV Cls"], new TextBoxUpdater(this.txtBoxCMD8TVCls));
                debugWinCMD8.RegisterObserver(ref debugWinCMD8.CMD8Observers, ObserversInfo.HARTCMD8Observers["QV Cls"], new TextBoxUpdater(this.txtBoxCMD8QVCls));

                await debugWinCMD8.ExecuteCMD();
            }
            else if (cboxUniversalCmd.Text == ObserversInfo.HARTCMDsID[12])
            {
                CMDsDebugWinHARTCMD12 debugWinCMD12 = new CMDsDebugWinHARTCMD12(MainForm.serialPortsManager);

                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg0"], new TextBoxUpdater(this.txtBoxMsg0));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg1"], new TextBoxUpdater(this.txtBoxMsg1));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg2"], new TextBoxUpdater(this.txtBoxMsg2));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg3"], new TextBoxUpdater(this.txtBoxMsg3));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg4"], new TextBoxUpdater(this.txtBoxMsg4));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg5"], new TextBoxUpdater(this.txtBoxMsg5));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg6"], new TextBoxUpdater(this.txtBoxMsg6));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg7"], new TextBoxUpdater(this.txtBoxMsg7));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg8"], new TextBoxUpdater(this.txtBoxMsg8));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg9"], new TextBoxUpdater(this.txtBoxMsg9));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg10"], new TextBoxUpdater(this.txtBoxMsg10));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg11"], new TextBoxUpdater(this.txtBoxMsg11));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg12"], new TextBoxUpdater(this.txtBoxMsg12));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg13"], new TextBoxUpdater(this.txtBoxMsg13));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg14"], new TextBoxUpdater(this.txtBoxMsg14));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg15"], new TextBoxUpdater(this.txtBoxMsg15));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg16"], new TextBoxUpdater(this.txtBoxMsg16));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg17"], new TextBoxUpdater(this.txtBoxMsg17));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg18"], new TextBoxUpdater(this.txtBoxMsg18));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg19"], new TextBoxUpdater(this.txtBoxMsg19));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg20"], new TextBoxUpdater(this.txtBoxMsg20));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg21"], new TextBoxUpdater(this.txtBoxMsg21));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg22"], new TextBoxUpdater(this.txtBoxMsg22));
                debugWinCMD12.RegisterObserver(ref debugWinCMD12.CMD12Observers, ObserversInfo.HARTCMD12Observers["Msg23"], new TextBoxUpdater(this.txtBoxMsg23));

                await debugWinCMD12.ExecuteCMD();
            }
            else if (cboxUniversalCmd.Text == ObserversInfo.HARTCMDsID[14])
            {
                CMDsDebugWinHARTCMD14 debugWinCMD14 = new CMDsDebugWinHARTCMD14(MainForm.serialPortsManager);

                debugWinCMD14.RegisterObserver(ref debugWinCMD14.CMD14Observers, ObserversInfo.HARTCMD14Observers["Transducer SN"], new TextBoxUpdater(this.txtBoxTransSN));
                debugWinCMD14.RegisterObserver(ref debugWinCMD14.CMD14Observers, ObserversInfo.HARTCMD14Observers["Transducer Li And Mini Span Unit"], new TextBoxUpdater(this.txtTransLimAndMiniSpanUnit));
                debugWinCMD14.RegisterObserver(ref debugWinCMD14.CMD14Observers, ObserversInfo.HARTCMD14Observers["Upper Transducer Limit"], new TextBoxUpdater(this.txtBoxUpperTransLimit));
                debugWinCMD14.RegisterObserver(ref debugWinCMD14.CMD14Observers, ObserversInfo.HARTCMD14Observers["Lower Transducer Limit"], new TextBoxUpdater(this.txtBoxLowerTransLimit));
                debugWinCMD14.RegisterObserver(ref debugWinCMD14.CMD14Observers, ObserversInfo.HARTCMD14Observers["Mini Span"], new TextBoxUpdater(this.txtBoxMinimumSpan));

                await debugWinCMD14.ExecuteCMD();
            }
            else if (cboxUniversalCmd.Text == ObserversInfo.HARTCMDsID[15])
            {
                CMDsDebugWinHARTCMD15 debugWinCMD15 = new CMDsDebugWinHARTCMD15(MainForm.serialPortsManager);

                debugWinCMD15.RegisterObserver(ref debugWinCMD15.CMD15Observers, ObserversInfo.HARTCMD15Observers["PV Alarm Sel"], new TextBoxUpdater(this.txtBoxPVAlmSelCode));
                debugWinCMD15.RegisterObserver(ref debugWinCMD15.CMD15Observers, ObserversInfo.HARTCMD15Observers["PV Transfer Fun"], new TextBoxUpdater(this.txtBoxPVTransFunCode));
                debugWinCMD15.RegisterObserver(ref debugWinCMD15.CMD15Observers, ObserversInfo.HARTCMD15Observers["PV Upper and Lower Rge Units"], new TextBoxUpdater(this.txtBoxPVULRgValCode));
                debugWinCMD15.RegisterObserver(ref debugWinCMD15.CMD15Observers, ObserversInfo.HARTCMD15Observers["PV Upper Range Val"], new TextBoxUpdater(this.txtBoxUpperRgVal));
                debugWinCMD15.RegisterObserver(ref debugWinCMD15.CMD15Observers, ObserversInfo.HARTCMD15Observers["PV Lower Range Val"], new TextBoxUpdater(this.txtBoxPVLowerRgVal));
                debugWinCMD15.RegisterObserver(ref debugWinCMD15.CMD15Observers, ObserversInfo.HARTCMD15Observers["PV Damping Value"], new TextBoxUpdater(this.txtBoxPVDampingVal));
                debugWinCMD15.RegisterObserver(ref debugWinCMD15.CMD15Observers, ObserversInfo.HARTCMD15Observers["Write Protect Code"], new TextBoxUpdater(this.txtBoxWriteProtectCode));
                debugWinCMD15.RegisterObserver(ref debugWinCMD15.CMD15Observers, ObserversInfo.HARTCMD15Observers["PV Analog Chl Flgs"], new TextBoxUpdater(this.txtBoxPVAnCgFlags));

                await debugWinCMD15.ExecuteCMD();
            }
        }
        private void cboxUniversalCmd_SelectedValueChanged(object sender, EventArgs e)
        {
            string currentCmdStr = cboxUniversalCmd.SelectedItem as string;

            if (currentCmdStr == ObserversInfo.HARTCMDsID[0])
            {
                tabControl.SelectedTab = tabPage0;
            }
            else if (currentCmdStr == ObserversInfo.HARTCMDsID[1])
            {
                tabControl.SelectedTab = tabPage0;
            }
            else if (currentCmdStr == ObserversInfo.HARTCMDsID[2])
            {
                tabControl.SelectedTab = tabPage0;
            }
            else if (currentCmdStr == ObserversInfo.HARTCMDsID[3])
            {
                tabControl.SelectedTab = tabPage0;
            }
            else if (currentCmdStr == ObserversInfo.HARTCMDsID[6])
            {
                tabControl.SelectedTab = tabPage0;
            }
            else if (currentCmdStr == ObserversInfo.HARTCMDsID[7])
            {
                tabControl.SelectedTab = tabPage0;
            }
            else if (currentCmdStr == ObserversInfo.HARTCMDsID[8])
            {
                tabControl.SelectedTab = tabPage1;
            }
            else if (currentCmdStr == ObserversInfo.HARTCMDsID[12])
            {
                tabControl.SelectedTab = tabPage1;
            }
            else if (currentCmdStr == ObserversInfo.HARTCMDsID[14])
            {
                tabControl.SelectedTab = tabPage1;
            }
            else if (currentCmdStr == ObserversInfo.HARTCMDsID[15])
            {
                tabControl.SelectedTab = tabPage2;
            }
        }
        private async void btnCMD6Config_Click(object sender, EventArgs e)
        {
            CMDsDebugWinHARTCMD6 debugWinCMD6 = new CMDsDebugWinHARTCMD6(MainForm.serialPortsManager, this.cboxPollingAddr.Text, this.cboxCMD6LoopCurMode.Text);
            await debugWinCMD6.ExecuteCMD();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MainForm.serialPortsManager.SetDebugMsgControl(this.txtBoxOutputInfo);

            // Command#0
            CMDsDebugWinHARTCMD0 CMD0 = new CMDsDebugWinHARTCMD0(MainForm.serialPortsManager);
            CMD0.RegisterObserver(ref CMD0.CMD0Observers, ObserversInfo.HARTCMD0Observers["All"], new GridViewUpdater(this.dataGridViewCmd0));
            // Command#1
            CMDsDebugWinHARTCMD1 CMD1 = new CMDsDebugWinHARTCMD1(MainForm.serialPortsManager);
            CMD1.RegisterObserver(ref CMD1.CMD1Observers, ObserversInfo.HARTCMD1Observers["PV Unit"], new TextBoxUpdater(this.cmd1UnitDisp));
            CMD1.RegisterObserver(ref CMD1.CMD1Observers, ObserversInfo.HARTCMD1Observers["PV"], new TextBoxUpdater(this.cmd1PreDisplay));
            // Command#2
            CMDsDebugWinHARTCMD2 CMD2 = new CMDsDebugWinHARTCMD2(MainForm.serialPortsManager);
            CMD2.RegisterObserver(ref CMD2.CMD2Observers, ObserversInfo.HARTCMD2Observers["PV Loop Current"], new TextBoxUpdater(this.txtBoxLoopCurrent));
            CMD2.RegisterObserver(ref CMD2.CMD2Observers, ObserversInfo.HARTCMD2Observers["PV Percent of Range"], new TextBoxUpdater(this.txtBoxPerOfRge));
            // Command#3
            CMDsDebugWinHARTCMD3 CMD3 = new CMDsDebugWinHARTCMD3(MainForm.serialPortsManager);
            CMD3.RegisterObserver(ref CMD3.CMD3Observers, ObserversInfo.HARTCMD3Observers["PV Loop Current"], new TextBoxUpdater(this.txtBoxPVLoopCur));
            CMD3.RegisterObserver(ref CMD3.CMD3Observers, ObserversInfo.HARTCMD3Observers["PV Units Code"], new TextBoxUpdater(this.txtBoxPVUnit));
            CMD3.RegisterObserver(ref CMD3.CMD3Observers, ObserversInfo.HARTCMD3Observers["PV"], new TextBoxUpdater(this.txtBoxPV));
            CMD3.RegisterObserver(ref CMD3.CMD3Observers, ObserversInfo.HARTCMD3Observers["SV Units Code"], new TextBoxUpdater(this.txtBoxSVUnitCode));
            CMD3.RegisterObserver(ref CMD3.CMD3Observers, ObserversInfo.HARTCMD3Observers["SV"], new TextBoxUpdater(this.txtBoxSV));
            CMD3.RegisterObserver(ref CMD3.CMD3Observers, ObserversInfo.HARTCMD3Observers["TV Units Code"], new TextBoxUpdater(this.txtBoxTVUnitCode));
            CMD3.RegisterObserver(ref CMD3.CMD3Observers, ObserversInfo.HARTCMD3Observers["TV"], new TextBoxUpdater(this.txtBoxTV));
            CMD3.RegisterObserver(ref CMD3.CMD3Observers, ObserversInfo.HARTCMD3Observers["QV Units Code"], new TextBoxUpdater(this.txtBoxQVUnitCode));
            CMD3.RegisterObserver(ref CMD3.CMD3Observers, ObserversInfo.HARTCMD3Observers["QV"], new TextBoxUpdater(this.txtBoxQV));

            CMDsQueue CommandsQueue = new CMDsQueue(MainForm.serialPortsManager);

            CommandsQueue.EnqueueCommand(CMD0, TimeSpan.FromSeconds(1));
            CommandsQueue.EnqueueCommand(CMD1, TimeSpan.FromSeconds(1));
            CommandsQueue.EnqueueCommand(CMD2, TimeSpan.FromSeconds(1));
            CommandsQueue.EnqueueCommand(CMD3, TimeSpan.FromSeconds(1));

            CommandsQueue.Start();
        }
        public void PrintfDebugMessage(string msg)
        {
            if (InvokeRequired)
            {
                Invoke(new Action<string>(PrintfDebugMessage), msg);
            }
            else
            { 
                this.txtBoxOutputInfo.AppendText(msg);
            }
        }

        private async void btnSetup_Click(object sender, EventArgs e)
        {
            SystemCMDSetup setupCMD = new SystemCMDSetup(MainForm.serialPortsManager);
            await setupCMD.ExecuteCMD();
        }
    }
}
